package com.sdry.web.controller.lz;

import com.sdry.model.lz.*;
import com.sdry.service.lz.MaterielService;
import com.sdry.service.lz.WarehouseService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * @ClassName MaterielController
 * @Description 物料信息
 * @Author lz
 * @Date 2019年4月15日 17:37:17
 * @Version 1.0
 */
@Controller
@RequestMapping("/materiel")
public class MaterielController {

    public static final Integer AFFECT_ROW = 0;

    @Resource
    MaterielService materielService;

    /**
     * 物料新增
     * @param response
     * @param materiel
     */
    @RequestMapping("/addMateriel")
    public void addMateriel(HttpServletResponse response, Materiel materiel){
        Long affectRow = materielService.addMateriel(materiel);
        //物料与仓库绑定时修改客户与仓库关联表的状态
        materielService.updateCustomerWarehouseStatus(materiel);
        try {
            ResponseUtil.write(response, affectRow);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据ID删除该行数据
     * @param response
     * @param idArr
     */
    @RequestMapping("/deleteMaterielById")
    public void deleteMaterielById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr){
        if(idArr.length > 0) {
            for (int i = 0; i < idArr.length; i++) {
                Long affectRow = materielService.deleteMaterielById(Long.parseLong(idArr[i]));
                if (affectRow > AFFECT_ROW) {
                    try {
                        ResponseUtil.write(response, affectRow);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        ResponseUtil.write(response, AFFECT_ROW);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 行内编辑物料表
     * @param response
     */
    @RequestMapping("/updateMateriel")
    public void updateMateriel(HttpServletResponse response, Materiel materiel) {
        Long affectRow = materielService.updateMateriel(materiel);
        if(affectRow > AFFECT_ROW){
            try {
                ResponseUtil.write(response, affectRow);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                ResponseUtil.write(response, "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 分页查询物料表
     * @param response
     * @param criteria
     */
    @RequestMapping("/queryMaterielCriteria")
    public void queryMaterielCriteria(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<Materiel> materielList = materielService.queryMaterielCriteria(criteria);
        int count = materielService.countMaterielCriteria(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(materielList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 查找所有容器
     * @param response
     * @throws Exception
     */
	@RequestMapping("/queryAllContainer")
	public void queryAllContainer(HttpServletResponse response) throws Exception{
		List<Container> containerList = materielService.queryAllContainer();
		JSONArray jsonArray = JSONArray.fromObject(containerList);
		if (containerList.size() > 0) {
            ResponseUtil.write(response, jsonArray);
        } else {
            ResponseUtil.write(response, null);
        }
	}
	/**
	 * 绑定客户
	 * @param materiel
	 * @param response
	 */
	/*@RequestMapping("bindingCustomer")
	public void bindingCustomer(Materiel materiel,HttpServletResponse response){
		int affact = materielService.bindingCustomer(materiel);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	/**
	 * 客户绑定仓库
	 * @param customer 客户实体
	 * @param response
	 */
	@RequestMapping("warehouseBindingWarehouse")
	public void warehouseBindingWarehouse(Customer customer, HttpServletResponse response){
		int affact = materielService.warehouseBindingWarehouse(customer);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 仓库树
	 */
	@RequestMapping("wareTree")
	public void wareTree(HttpServletResponse response,String regionType){
		//仓库
		List<Warehouse> warehouse = materielService.selectAllWarehouse();
		JSONArray jsonArray1 =  new JSONArray();
		JSONArray jsonArray2 =  new JSONArray();
		JSONArray jsonArray3 =  new JSONArray();
		for (Warehouse w : warehouse) {
			JSONObject json=new JSONObject();
			JSONObject jsonemp=new JSONObject();
			JSONObject jsonempchild=new JSONObject();
			json.put("name", w.getWarehouse_name());
			json.put("value", w.getId());
			//库区
			List<WarehouseRegion> region = materielService.selectRegionByWareid(w.getId());
			for (WarehouseRegion r : region) {
				 jsonemp.put("name", r.getRegion_name());
				 jsonemp.put("value", r.getId());
				 if("0".equals(regionType)){
					 //库位
					 List<WarehouseRegionLocation> location = materielService.selectLocationByRegionid(r.getId());
					 for (WarehouseRegionLocation l : location) {
						 jsonempchild.put("name", l.getLocation_name());
						 jsonempchild.put("value", l.getId());
						 jsonArray3.add(jsonempchild);
						 jsonemp.put("children", jsonArray3);
					 }
				 }
				 jsonArray2.add(jsonemp);
				 json.put("children",jsonArray2);
			}
			jsonArray1.add(json);
		}
		JSONObject jsonObject = new JSONObject();
        jsonObject.put("rows", jsonArray1);
        try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据客户id查询关联表中的库区
	 * @param response
	 * @param customer_id 客户id
	 * @throws Exception
	 */
	@RequestMapping("/queryRegionByCustomerId")
	public void queryRegionByCustomerId(HttpServletResponse response, String customer_id) throws Exception{
		List<WarehouseRegion> warehouseRegionList = materielService.queryRegionByCustomerId(customer_id);
		JSONArray jsonArray = JSONArray.fromObject(warehouseRegionList);
		if (warehouseRegionList.size() > 0) {
            ResponseUtil.write(response, jsonArray);
        } else {
            ResponseUtil.write(response, null);
        }
	}
}
